/*
 * @Description: 回收站reducer
 * @Author: Rfan
 * @Date: 2022-10-28 13:55:59
 * @LastEditTime: 2023-03-01 21:03:19
 */

import { delItemByIdArray } from '@/utils/list';
import {
  CLEAR_ALL,
  DELETE_RECYCLE_ITEM,
  RESTORE_ITEM,
  SET_RECYCLE_LIST,
  SET_SELECTED_ITEM
} from '../constants';

export const FavouriteReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 设置回收站条目
     */
    case SET_RECYCLE_LIST:
      return {
        ...state,
        list: action.data
      };

    /**
     * @description: 删除回收站条目
     */
    case DELETE_RECYCLE_ITEM:
      return {
        ...state,
        list: delItemByIdArray(action.ids, state.list)
      };

    /**
     * @description: 恢复成功删除对应条目
     */
    case RESTORE_ITEM:
      return {
        ...state,
        list: delItemByIdArray(action.ids, state.list)
      };

    /**
     * @description: 清空列表
     */
    case CLEAR_ALL:
      return {
        ...state,
        list: []
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
